package com.cby.assist.kits.core;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.cby.assist.kits.IKit;
import com.cby.assist.kits.R;
import com.cby.assist.kits.ui.MainKitsActivity;
import com.cby.assist.kits.widget.floatview.FloatWindow;
import com.cby.assist.kits.widget.floatview.PermissionListener;
import com.cby.assist.kits.widget.floatview.Screen;
import com.cby.assist.kits.widget.floatview.ViewStateListenerAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @description: OliveManager
 * @author: ChenBaoYang
 * @createDate: 2022/3/7 1:56 下午
 */
public class OliveManager {

    private OliveManager() {}

    private static class SingleTon {
        private static final OliveManager INSTANCE = new OliveManager();
        private SingleTon(){}
    }

    public static OliveManager getInstance() {
        return SingleTon.INSTANCE;
    }

    /**
     * 设置全局悬浮按钮
     * @param context 上下文
     * @param classes 用于
     */
    public void setFloat(Context context, List<Class<?>> classes) {
        ImageView imageView = new ImageView(context.getApplicationContext());
        imageView.setImageResource(R.drawable.ic_float_olive);
        FloatWindow
                .with(context)
                .setView(imageView)
                //设置悬浮控件宽高
                .setWidth(Screen.width, 0.2f)
                .setHeight(Screen.width, 0.2f)
                //设置控件初始位置
                .setX(Screen.width, 0.8f)
                .setY(Screen.height, 0.3f)
                //桌面显示
                .setDesktopShow(false)
                //设置这两个页面隐藏
                .setFilter(
                        false, classes.toArray(new Class[classes.size()])
                ) //监听悬浮控件状态改变
                .setViewStateListener(new ViewStateListenerAdapter())
                //监听权限申请结果
                .setPermissionListener(new PermissionListener() {
                    @Override
                    public void onSuccess() {
                        Log.i("CbyTool","NetworkTool-------PermissionListener------onSuccess");
                    }

                    @Override
                    public void onFail() {
                        Log.i("CbyTool","NetworkTool-------PermissionListener------onFail");
                    }

                })
                .build();
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                start(context);
            }
        });
    }


    private static void start(Context context) {
        try {
            Intent intent = new Intent(context, MainKitsActivity.class);
            //为activity开启新的栈，Intent.FLAG_ACTIVITY_NEW_TASK 设置状态，
            //首先查找是否存在和被启动的Activity具有相同的任务栈，如果有则直接把这个栈整体移到前台，并保持栈中的状态不变，
            //既栈中的activity顺序不变，如果没有，则新建一个栈来存放被启动的Activity。
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
